package com.kwxy.hyt.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kwxy.hyt.base.BaseController;
import com.kwxy.hyt.po.*;
import com.kwxy.hyt.utils.Pager;
import java.util.*;

import com.kwxy.hyt.po.*;
import com.kwxy.hyt.mapper.*;
import com.kwxy.hyt.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author  - - admin
 * @date - 2020年9月10日 09时48分24秒
 */


@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
	
	
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private TeacherService teacherService;
	
	@Autowired
	private BjService bjService;
	
	@Autowired
	private TeacherBjService teacherBjService;
	
	
	// --------------------------------------- 华丽分割线 ------------------------------
	
	
	
	
	/**
	 * 分页查询 返回list对象(通过对象)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByObj")
	public String findByObj(Teacher teacher, Model model, HttpServletRequest request, HttpServletResponse response) {
		//分页查询
		Pager<Teacher> pagers = teacherService.findByEntity(teacher);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", teacher);
		return "teacher/teacher";
	}
	
	/**
	 * 分页查询 返回list对象(通过对By Sql)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findBySql")
	public String findBySql(Teacher teacher, Model model, HttpServletRequest request, HttpServletResponse response) {
		//分页查询
		String sql = "SELECT * FROM teacher WHERE isDelete = 0 ";
        if(!isEmpty(teacher.getCode())){
        	sql += " and code like '%"+teacher.getCode()+"%'";
		}
        if(!isEmpty(teacher.getUsername())){
        	sql += " and username like '%"+teacher.getUsername()+"%'";
		}
        if(!isEmpty(teacher.getPassword())){
        	sql += " and password like '%"+teacher.getPassword()+"%'";
		}
        if(!isEmpty(teacher.getKc())){
        	sql += " and kc like '%"+teacher.getKc()+"%'";
		}
        if(!isEmpty(teacher.getIsDelete())){
        	sql += " and isDelete like '%"+teacher.getIsDelete()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Teacher> pagers = teacherService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", teacher);
		return "teacher/teacher";
	}
	
	
	/**
	 * 分页查询 返回list对象(通过Map)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByMap")
	public String findByMap(Teacher teacher, Model model, HttpServletRequest request, HttpServletResponse response) {
		//通过map查询
		Map<String,Object> params = new HashMap<String,Object>();
        if(!isEmpty(teacher.getCode())){
        	params.put("code", teacher.getCode());
		}
        if(!isEmpty(teacher.getUsername())){
        	params.put("username", teacher.getUsername());
		}
        if(!isEmpty(teacher.getPassword())){
        	params.put("password", teacher.getPassword());
		}
        if(!isEmpty(teacher.getKc())){
        	params.put("kc", teacher.getKc());
		}
        if(!isEmpty(teacher.getIsDelete())){
        	params.put("isDelete", teacher.getIsDelete());
		}
		//分页查询
		Pager<Teacher> pagers = teacherService.findByMap(params);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", teacher);
		return "teacher/teacher";
	}
	
	/**********************************【增删改】******************************************************/
	
	/**
	 * 跳至添加页面
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add(Model model) {
		// 查询班级列表
				Bj b = new Bj();
				b.setIsDelete(0);
				List<Bj> listAllByEntity = bjService.listAllByEntity(b);
				model.addAttribute("bjs", listAllByEntity);
		return "teacher/add";
	}

	@RequestMapping(value = "/pass")
	public String pass(Integer id,HttpServletRequest request,Model model) {
       Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		Teacher obj = teacherService.load(userId);
		model.addAttribute("obj",obj);
		return "teacher/pass";
	}
	
	
	@RequestMapping("/upass")
	@ResponseBody
	public String upass(HttpServletRequest request,String password){
      Object attribute = request.getSession().getAttribute("userId");
      JSONObject j = new  JSONObject();
	   	if (attribute == null){
	   		j.put("res", 0);
	   		return j.toString();
		}
			Integer userId = Integer.valueOf(attribute.toString());
			Teacher byId = teacherService.getById(userId);
			byId.setPassword(password);
			teacherService.updateById(byId);
			j.put("res", 1);
			return j.toString();
			
	}
	
	/**
	 * 跳至详情页面
	 * @return
	 */
	@RequestMapping(value = "/view")
	public String view(Integer id,Model model) {
		Teacher obj = teacherService.load(id);
		model.addAttribute("obj",obj);
		return "teacher/view";
	}
	
	/**
	 * 添加执行
	 * @return
	 */
	@RequestMapping(value = "/exAdd")
	public String exAdd(Teacher teacher,@RequestParam(value = "ids") Integer[] ids, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		if (ids != null && ids.length > 0){
			teacher.setIsDelete(0);
			teacherService.insert(teacher);
			for (Integer bId : ids){
				TeacherBj tb = new TeacherBj();
				tb.setBjId(bId);
				tb.setTeacherId(teacher.getId());
				teacherBjService.insert(tb);
			}
		}
		
		
		return "redirect:/teacher/findBySql.action";
	}
	
	
	/**
	 * 跳至修改页面
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id,Model model) {
		Teacher obj = teacherService.load(id);
		// 查询班级列表
		Bj b = new Bj();
		b.setIsDelete(0);
		List<Bj> listAllByEntity = bjService.listAllByEntity(b);
		//查看教师已经选中的
		TeacherBj t = new TeacherBj();
		t.setTeacherId(id);
		List<TeacherBj> select = teacherBjService.listAllByEntity(t);
		
		List<Integer> ids = new ArrayList<Integer>();
		
		if (!CollectionUtils.isEmpty(select)){
			for (TeacherBj b2 : select){
				ids.add(b2.getBjId());
			}
		}
		
		if (!CollectionUtils.isEmpty(listAllByEntity)){
			
			for (Bj bj : listAllByEntity){
				
				if (!CollectionUtils.isEmpty(ids) && ids.contains(bj.getId())){
					bj.setIsCheck(1);
				}else{
					bj.setIsCheck(0);
				}
			}
		}
		
		
		model.addAttribute("bjs", listAllByEntity);
		model.addAttribute("obj",obj);
		return "teacher/update";
	}
	
	/**
	 * 添加修改
	 * @return
	 */
	@RequestMapping(value = "/exUpdate")
	public String exUpdate(Teacher teacher,@RequestParam(value = "ids") Integer[] ids, Model model, HttpServletRequest request, HttpServletResponse response) {
		teacherService.updateById(teacher);
		if (ids != null && ids.length > 0){
			teacher.setIsDelete(0);
			//删除
			TeacherBj t = new TeacherBj();
			t.setTeacherId(teacher.getId());
			teacherBjService.deleteByEntity(t);
			for (Integer bId : ids){
				TeacherBj tb = new TeacherBj();
				tb.setBjId(bId);
				tb.setTeacherId(teacher.getId());
				teacherBjService.insert(tb);
			}
		}
		return "redirect:/teacher/findBySql.action";
	}
	
	/**
	 * 删除通过主键
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) {
		Teacher load = teacherService.load(id);
		TeacherBj t = new TeacherBj();
		t.setTeacherId(id);
		teacherBjService.deleteByEntity(t);
		load.setIsDelete(1);
		teacherService.updateById(load);
		return "redirect:/teacher/findBySql.action";
	}
	
	
}
